package com.industrial.premu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuPassword;
import com.industrial.premu.service.IPremuPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 导入密码Controller
 * 
 * @author Zhou_S
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/premuPassword")
public class PreMuPasswordController extends BaseController
{
    @Autowired
    private IPremuPasswordService preMuPasswordService;

    /**
     * 查询导入密码列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PreMuPassword preMuPassword)
    {
        startPage();
        List<PreMuPassword> list = preMuPasswordService.selectPreMuPasswordList(preMuPassword);
        return getDataTable(list);
    }

    /**
     * 导出导入密码列表
     */
    @Log(title = "导入密码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuPassword preMuPassword)
    {
        List<PreMuPassword> list = preMuPasswordService.selectPreMuPasswordList(preMuPassword);
        ExcelUtil<PreMuPassword> util = new ExcelUtil<PreMuPassword>(PreMuPassword.class);
        util.exportExcel(response, list, "导入密码数据");
    }

    /**
     * 获取导入密码详细信息
     */

    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuPasswordService.selectPreMuPasswordByBaseId(baseId));
    }

    /**
     * 新增导入密码
     */
    @RequiresPermissions("@ss.hasPermi('system:password:add')")
    @Log(title = "导入密码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuPassword preMuPassword)
    {
        return toAjax(preMuPasswordService.insertPreMuPassword(preMuPassword));
    }

    /**
     * 修改导入密码
     */
    @Log(title = "导入密码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuPassword preMuPassword)
    {
        if(preMuPassword.getOldPassword()!=null){
            PreMuPassword    oldpreMuPassword=   preMuPasswordService.selectByOldPassWord(preMuPassword.getOldPassword());
            if(oldpreMuPassword!=null){
                return toAjax(preMuPasswordService.updatePreMuPassword(preMuPassword));
            }else{
                return warn("原始密码错误！");
            }

        }else{
            return warn("未输入原始密码！");
        }


    }

    /**
     * 删除导入密码
     */
    @RequiresPermissions("@ss.hasPermi('system:password:remove')")
    @Log(title = "导入密码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuPasswordService.deletePreMuPasswordByBaseIds(baseIds));
    }
}
